<?php namespace App\Http\Controllers\Api\Admin;

use App\Admin;
use App\AdvertImg;
use App\Banner;
use App\BannerNav;
use App\BooksChapter;
use App\BooksInfo;
use App\Classify;
use App\Coupons;
use App\Express;
use App\Http\Controllers\Controller;
use App\QrCodes;
use App\RechargePackage;

use App\UserInfo;

use App\ShowGoods;
use Auth;
use Illuminate\Http\Request;

class SystemController extends Controller
{
    public function setBanner(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $title   = $request->get('title');
        $linkUrl = $request->get('link_url');
        $appId   = $request->get('app_id');
        $sort    = $request->get('sort');
        $imgId   = $request->get('img_id');
        $id      = !is_null($request->get('id'))?$request->get('id'):0;
        $type    = $request->get('type');

        $thatBanner = Banner::whereId(trim($id))->first();

        if(!$thatBanner)
            $thatBanner = new Banner();

        if(!is_null($title))$thatBanner->title      = $title;
        if(!is_null($linkUrl))$thatBanner->link_url = $linkUrl;
        if(!is_null($appId))$thatBanner->app_id     = $appId;
        if(!is_null($sort))$thatBanner->sort        = $sort;
        if(!is_null($imgId))$thatBanner->img_id     = $imgId;
        if(!is_null($type))$thatBanner->type        = $type;
        $thatBanner->save();

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$thatBanner]);
    }

    public function delBanner(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);


        $id = !is_null($request->get('id'))? $request->get('id'):0;

        /** @var Banner $thatBanner */
        $thatBanner = Banner::whereId($id)->first();

        if(!$thatBanner)
            return response()->json(["level"=>"error","message"=>"该banner不存在！"]);

        $thatBanner->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatBanner]);
    }

    public function setAdvertImg(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $title   = $request->get('title');
        $linkUrl = $request->get('link_url');
        $sort    = $request->get('sort');
        $imgId   = $request->get('img_id');
        $type    = $request->get('type');
        $id      = !is_null($request->get('id'))?$request->get('id'):0;
        $showGoodsTwo = $request->get('show_goods_two');


        $thatAdvertImg=AdvertImg::whereId($id)->first();

        if(!$thatAdvertImg)
            $thatAdvertImg = new AdvertImg();

        if(!is_null($title))$thatAdvertImg->title      = $title;
        if(!is_null($linkUrl))$thatAdvertImg->link_url = $linkUrl;
        if(!is_null($sort))$thatAdvertImg->sort        = $sort;
        if(!is_null($type))$thatAdvertImg->type        = $type;
        if(!is_null($imgId))$thatAdvertImg->img_id     = $imgId;
        $thatAdvertImg->save();

        $thisShowGoods = ShowGoods::whereId(1)->first();
        if(!$thisShowGoods)
            $thisShowGoods = new ShowGoods();

        if(!is_null($showGoodsTwo))$thisShowGoods->show_goods_two = $showGoodsTwo;
        $thisShowGoods->save();

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$thatAdvertImg]);
    }

    public function delAdvertImg(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var Banner $thatBanner */
        $thatAdvertImg = AdvertImg::whereId($id)->first();

        if(!$thatAdvertImg)
            return response()->json(["level"=>"error","message"=>"该banner不存在！"]);

        $thatAdvertImg->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatAdvertImg]);
    }

    public function setAdmin(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $username = $request->get('username');
        $password = $request->get('password');
        $repass   = $request->get('repass');

        if($password != $repass)
            return response()->json(['level'=>'error','message' => '两次密码不一致！']);

        /** @var Admin $hasUser */
        $hasUser = Admin::whereUsername($username)->first();
        if($hasUser){
            return response()->json(['level'=>'error','message' => '当前管理员账号已存在']);
        }

        $thatAdmin = new Admin();
        $thatAdmin->username = $username;
        $thatAdmin->password = bcrypt($password);
        $thatAdmin->status = 2;
        $thatAdmin->save();

        return response()->json(['level'=>'success','message'=>'添加成功','data'=>$thatAdmin]);
    }

    public function delAdmin(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var Admin $thatAdmin */
        $thatAdmin = Admin::whereId($id)->first();

        if(!$thatAdmin)
            return response()->json(["level"=>"error","message"=>"该管理员不存在！"]);

        if($thatAdmin->status == 1)
            return response()->json(["level"=>"error","message"=>"该管理员不能删除！"]);


        $thatAdmin->forceDelete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatAdmin]);
    }

    public function addClassify(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $name = $request->get("name");
        $sort = $request->get('sort');
        $id   = !is_null($request->get('id'))?$request->get('id'):0;

        $newClassify = Classify::whereId(trim($id))->first();

        if(!$newClassify)
            $newClassify = new Classify();

        if(!is_null($name))$newClassify->name = $name;
        if(!is_null($sort))$newClassify->sort = $sort;
        $newClassify->save();

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newClassify]);
    }

    public function delClassify(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;
        $thatClassify =Classify::whereId($id)->first();

        if(!$thatClassify)
            return response()->json(["level"=>"error","message"=>"该分类不存在！"]);

        $thatClassify->delete();

        return response()->json(['level' => 'success','message' => '删除成功']);
    }

    public function setCoupons(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $remark         =$request->request->get('remark');
        $name           = $request->get('name');
        $money          = $request->get('money');
        $startTime      = $request->get('start_time');
        $endTime        = $request->get('end_time');
        $type           = $request->get('type');
        $satisfyMoney   = $request->get('satisfy_money');
        $num            = $request->get('num');
        $status         = $request->get('status');
        $imgId          = $request->get('imgId');
        $id             = !is_null($request->get('id')) ? $request->get('id') : 0;

        $newCoupon = Coupons::whereId($id)->first();
        if(!$newCoupon)
            $newCoupon = new Coupons();

        if(!is_null($name))$newCoupon->name                        = $name;
        if(!is_null($money))$newCoupon->money                      = $money;
        if(!is_null($startTime))$newCoupon->start_time             = $startTime;
        if(!is_null($endTime))$newCoupon->end_time                 = $endTime;
        if(!is_null($type))$newCoupon->type                        = $type;
        if(!is_null($satisfyMoney))$newCoupon->satisfy_money       = $satisfyMoney;
        if(!is_null($num))$newCoupon->num                          = $num;
        if(!is_null($status))$newCoupon->status                    = $status;
        if(!is_null($remark))$newCoupon->remark                    =$remark;
        if(!is_null($imgId))$newCoupon->img_id                    =$imgId;
        $newCoupon->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $newCoupon]);
    }

    public function delCoupons(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thatCoupon = Coupons::whereId($id)->first();
        if(!$thatCoupon)
            return response()->json(['level' => 'error','message' => '该优惠群不存在']);

        $thatCoupon->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thatCoupon]);
    }


    public function delBannerNav(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);


        $id = !is_null($request->get('id'))? $request->get('id'):0;

        /** @var Banner $thatBanner */
        $thatBanner = BannerNav::whereId($id)->first();

        if(!$thatBanner)
            return response()->json(["level"=>"error","message"=>"该banner不存在！"]);

        $thatBanner->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatBanner]);
    }

    public function setBannerNav(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $name   = $request->get('name');
        $linkUrl = $request->get('link');
        $imgId   = $request->get('img_id');
        $id      = !is_null($request->get('id'))?$request->get('id'):0;


        $thatBanner = BannerNav::whereId(trim($id))->first();

        if(!$thatBanner)
            $thatBanner = new BannerNav();

        if(!is_null($name))$thatBanner->name = $name;
        if(!is_null($linkUrl))$thatBanner->link_url = $linkUrl;
        if(!is_null($imgId))$thatBanner->img_id = $imgId;

        $thatBanner->save();

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$thatBanner]);
    }

    public  function setExpressCompany(Request $request){

        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id=!is_null($request->get('id'))?$request->get('id'):0;
        $name=$request->get('name');
        $fee=$request->get('fee');

        $thatExpressCompany=Express::whereId($id)->first();

        if(!$thatExpressCompany)

            $thatExpressCompany=new Express();

        if(!is_null($name))$thatExpressCompany->name = $name;
        if(!is_null($fee))$thatExpressCompany->fee=$fee;
        $thatExpressCompany->save();

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$thatExpressCompany]);
    }

    public  function  delExpressCompany(Request $request){

        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))? $request->get('id'):0;

        $thatExpressCompany=Express::whereId($id)->first();

        if(!$thatExpressCompany)

            return response()->json(["level"=>"error","message"=>"该快递公司不存在！"]);

        $thatExpressCompany->delete();

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$thatExpressCompany]);

    }

    public function setExpressUse(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);
        $id = $request->get("id");

      $allExpress=Express::orderBy('created_at','desc')->get();

        if(count($allExpress)>0){
            foreach ($allExpress  as $express){
                $express->is_default=0;
                $express->save();
            }

        }

        $thisExpress=Express::whereId($id)->first();
        if(!$thisExpress)
            return response()->json(["level"=>"error","message"=>"快递公司不存在！"]);

   /*     if($thisExpress->is_default == 1){
            $thisExpress->is_default = 0;
            $thisExpress->save();
        }else{
            $thisExpress->is_default = 1;
            $thisExpress->save();
        }*/

        $thisExpress->is_default=1;
        $thisExpress->save();
            return response()->json(["level"=>"success","message"=>"启用成功","data"=>$thisExpress]);
    }

    public function setQrCode(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录！']);

        $key      = $request->get('key');
        $url      = $request->get('url');
        $integral = $request->get('integral');
        $status   = $request->get('status');
        $id       = !is_null($request->get('id')) ? $request->get('id') : 0;

        $newQrCode = QrCodes::whereId($id)->first();
        if(!$newQrCode)
            $newQrCode = new QrCodes();

        if(!is_null($key))$newQrCode->key           = $key;
        if(!is_null($url))$newQrCode->url           = $url;
        if(!is_null($integral))$newQrCode->integral = $integral;
        if(!is_null($status))$newQrCode->status     = $status;
        $newQrCode->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $newQrCode]);
    }

    public function delQrCode(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thisQrCode = QrCodes::whereId($id)->first();
        if(!$thisQrCode)
            return response()->json(['level' => 'error','message' => '该二维码不存在!']);

        $thisQrCode->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thisQrCode]);
    }

    public  function test(Request $request){

        $idCardNum=$request->get('card');
        $phone=$request->get('phone');
        $test=$request->get('test');
        $userinfo=UserInfo::whereId(1)->first();

        $preg_phone='/^1[345789]\d{9}$/ims';

        if(is_null($phone)||!preg_match('/^1[345789]\d{9}$/ims',$phone))
            return response()->json(['level' => 'error', 'message' => '手机号码格式不正确!']);

        if(strpos($test," "))     {
            return response()->json(['level' => 'error', 'message' => '请输入正确格式!']);
    }

     /*   if (!preg_match('/^\d{15}$)|(^\d{17}([0-9]|X)$/isu',$idCardNum))
            return response()->json(['level' => 'error', 'message' => '身份证号格式不正确']);*/

        $userinfo->realname=$test;
        return response()->json(['level' => 'success','message' => '修改成功','data' => $userinfo]);
    }
}